<?php  if ( ! defined('BASEPATH')) exit('No direct script access allowed');
/*
|--------------------------------------------------------------------------
| bash_helper.php
|--------------------------------------------------------------------------
| auther:Liutiansi
| Email:liutiansi@gmail.com
| update:2008-08-03
|
*/

/**
 * Parse out the attributes
 *
 * Some of the functions use this
 *
 * @access	private
 * @param	array
 * @param	bool
 * @return	string
 */
if ( ! function_exists('JSalert'))
{
	function JSalert($str, $reload ="null")
	{
		$S="<script language=\"javascript\">\n";  
		$S.="	ShowAlert = function(){\n";
		$S.="alert('".$str."');\n";
		if ($reload!="null")
			$S.="window.location.href='".$reload."';\n";		
		$S.="}\n";
		$S.="ShowAlert()\n";
		$S.="</script>\n";
		return $S;
	}
}

/*
|--------------------------------------------------------------------------
| Get folder file list
|--------------------------------------------------------------------------
|
|  Get folder file list
| 
|
*/

function get_file_list($folder)
{
        $fp=opendir($folder);
        $file_list_str="<option value=null>选择应用名称</option>";
        while(false!=$file=readdir($fp))
        {
            if($file!='.' &&$file!='..')
            {
                $file="$file";
                $arr_file[]=$file;
                }
        }

        //输出结果
        if(is_array($arr_file))
         {
            while(list($key,$value)=each($arr_file))
            {
                $file_list_str.="<option value=".$value.">".$value."</option>\n";
            }
        }
        closedir($fp);
        return $file_list_str;
}


/*
|--------------------------------------------------------------------------
| check url
|--------------------------------------------------------------------------
|
|  check url.
| 
|
*/	 	
	function check_url($str)
	{
                $pattern = "/^http:\/\/[A-Za-z0-9]+\.[A-Za-z0-9]+[\/=\?%\-&_~`@[\]\':+!]*([^\"\"])*$/";      
                return (preg_match($pattern,$str))?true:false;
	}


/*
|--------------------------------------------------------------------------
| write file
|--------------------------------------------------------------------------
|
|  write file
| 
|
*/	 	
function writeFile($filename,$data,$method="w") { 
	$file=fopen($filename,$method);
	flock($file,LOCK_EX); 
	$filedata=fwrite($file,$data); 
	fclose($file); 
	return $filedata; 
}



/*
|--------------------------------------------------------------------------
| IP to INT
|--------------------------------------------------------------------------
|
|  IP to INT
| 
|
*/
function ip2int($ip){
   list($ip1,$ip2,$ip3,$ip4)=explode(".",$ip); 
   return $ip1*pow(256,3)+$ip2*pow(256,2)+$ip3*256+$ip4; 
} 


/*
|--------------------------------------------------------------------------
| getPerformance HTML
|--------------------------------------------------------------------------
|
|  getPerformance HTML
| 
|
*/	 	
function getPerformance($type,$lvsip,$ip="") { 
	$HTML="<table width=\"98%\" align=center border=0>\n";
	if ($ip!="")
		$ip=strtoupper(dechex(ip2int($ip)));
	if ($type=="lvsip")
	{
		$HTML.="<tr height=425 align=center><td><img src=\"/images/LVS/".$lvsip."/lvs.All.All.All.All.All.gif\"></td></tr>\n";
		$HTML.="<tr height=425 align=center><td><img src=\"/images/LVS/".$lvsip."/lvs.All.All.All.All.All-day.gif\"></td></tr>\n";
		$HTML.="<tr height=425 align=center><td><img src=\"/images/LVS/".$lvsip."/lvs.All.All.All.All.All-week.gif\"></td></tr>\n";
		$HTML.="<tr height=425 align=center><td><img src=\"/images/LVS/".$lvsip."/lvs.All.All.All.All.All-month.gif\"></td></tr>\n";
		$HTML.="<tr height=425 align=center><td><img src=\"/images/LVS/".$lvsip."/lvs.All.All.All.All.All-year.gif\"></td></tr>\n";
	}elseif ($type=="vip")
	{
		$HTML.="<tr height=425 align=center><td><img src=\"/images/LVS/".$lvsip."/lvs.".$ip.".All.All.All.All.gif\"></td></tr>\n";
		$HTML.="<tr height=425 align=center><td><img src=\"/images/LVS/".$lvsip."/lvs.".$ip.".All.All.All.All-day.gif\"></td></tr>\n";
		$HTML.="<tr height=425 align=center><td><img src=\"/images/LVS/".$lvsip."/lvs.".$ip.".All.All.All.All-week.gif\"></td></tr>\n";
		$HTML.="<tr height=425 align=center><td><img src=\"/images/LVS/".$lvsip."/lvs.".$ip.".All.All.All.All-month.gif\"></td></tr>\n";
		$HTML.="<tr height=425 align=center><td><img src=\"/images/LVS/".$lvsip."/lvs.".$ip.".All.All.All.All-year.gif\"></td></tr>\n";
	}
	elseif ($type=="server")
	{
		$HTML.="<tr height=425 align=center><td><img src=\"/images/LVS/".$lvsip."/lvs.All.All.".$ip.".All.All.gif\"></td></tr>\n";
		$HTML.="<tr height=425 align=center><td><img src=\"/images/LVS/".$lvsip."/lvs.All.All.".$ip.".All.All-day.gif\"></td></tr>\n";
		$HTML.="<tr height=425 align=center><td><img src=\"/images/LVS/".$lvsip."/lvs.All.All.".$ip.".All.All-week.gif\"></td></tr>\n";
		$HTML.="<tr height=425 align=center><td><img src=\"/images/LVS/".$lvsip."/lvs.All.All.".$ip.".All.All-month.gif\"></td></tr>\n";
		$HTML.="<tr height=425 align=center><td><img src=\"/images/LVS/".$lvsip."/lvs.All.All.".$ip.".All.All-year.gif\"></td></tr>\n";
	}
	$HTML.="</table>\n";
	return $HTML;
}


/*
|--------------------------------------------------------------------------
| check email
|--------------------------------------------------------------------------
|
|  check email.
| 
|
*/	 	
	function check_email($email)
	{
	        $exp = "^[a-z'0-9]+([._-][a-z'0-9]+)*@([a-z0-9]+([._-][a-z0-9]+))+$";
	        if(eregi($exp,$email))
		{ 
	                if(checkdnsrr(array_pop(explode("@",$email)),"MX"))
	                {
	                        return true;
	                }else{
	                        return false;
	                }
	        }else{  
	                return false;
		}
	}




/*
|--------------------------------------------------------------------------
| check mobile
|--------------------------------------------------------------------------
|
|  check mobile.
| 
|
*/	 	
	function check_mobile($str)
	{
		if(!ereg("^1[3|5][0-9]{9}$", $str))
		{
			return false;
		}
		return true;
	}



/*
|--------------------------------------------------------------------------
| out Monitors parname
|--------------------------------------------------------------------------
|
|  out Monitors parname
| 
|
*/
        function  MonitorsParname($montype,$parid)  
        {
		switch ($montype)
		{
			case 'HTTP_GET':
			case 'SSL_GET':
				if ($parid=="1")
					return "path";
				elseif ($parid=="2")
					return "digest";
				break;
			case 'TCP_CHECK':
				if ($parid=="1")
					return "connect_port";
				elseif ($parid=="2")
					return "connect_timeout";
				break;
			case 'SMTP_CHECK':
				if ($parid=="1")
					return "connect_port";
				elseif ($parid=="2")
					return "connect_timeout";
				break;
			case 'MISC_CHECK':
				if ($parid=="1")
					return "misc_path";
				elseif ($parid=="2")
					return "misc_timeout";
				break;
		}
        } 


/*
|--------------------------------------------------------------------------
| return web condition color
|--------------------------------------------------------------------------
|
|  
| 
|
*/
        function  GetwebConditionColor($ConditionKey)  
        {
		$color_array[0][0]="soc";
		$color_array[0][1]="#E68604";
		$color_array[1][0]="302";
		$color_array[1][1]="#97ECB5";
		$color_array[2][0]="403";
		$color_array[2][1]="#E438E4";
		$color_array[3][0]="400";
		$color_array[3][1]="#113DEA";
		$color_array[4][0]="Not";
		$color_array[4][1]="#fFA813";
		$color_array[5][0]="503";
		$color_array[5][1]="#FA2953";
		$color_array[6][0]="200";
		$color_array[6][1]="#9ec5aa";		
		$focuskey=substr($ConditionKey,0,3);
		for ($i=0;$i<7;$i++)
			if ($color_array[$i][0]==$focuskey)
				return $color_array[$i][1];
        }

	
	
/*
|--------------------------------------------------------------------------
|  cut string
|--------------------------------------------------------------------------
|
|  support utb-8 and gbk string
| 
|
*/
function cut_str($string, $sublen, $start = 0, $code = 'UTF-8') 
{
         if($code == 'UTF-8') 
         {
                   $pa = "/[\x01-\x7f]|[\xc2-\xdf][\x80-\xbf]|\xe0[\xa0-\xbf][\x80-\xbf]|[\xe1-\xef][\x80-\xbf][\x80-\xbf]|\xf0[\x90-\xbf][\x80-\xbf][\x80-\xbf]|[\xf1-\xf7][\x80-\xbf][\x80-\xbf][\x80-\xbf]/"; 
                   preg_match_all($pa, $string, $t_string); 
    
                   if(count($t_string[0]) - $start > $sublen) return join('', array_slice($t_string[0], $start, $sublen))."..."; 
                   return join('', array_slice($t_string[0], $start, $sublen)); 
         }
         else
         { 

                   $start = $start*2; 
                   $sublen = $sublen*2; 
                   $strlen = strlen($string); 
                   $tmpstr = ''; 
    
                   for($i=0; $i<$strlen; $i++) 
                   {
                             if($i>=$start && $i<($start+$sublen)) 
                             {
                                       if(ord(substr($string, $i, 1))>129) 
                                       {
                                                 $tmpstr.= substr($string, $i, 2); 
                                       }
                                       else
                                       { 
                                                 $tmpstr.= substr($string, $i, 1); 
                                       }
                             }
                             if(ord(substr($string, $i, 1))>129) $i++; 
                   }
                   if(strlen($tmpstr)<$strlen ) $tmpstr.= "..."; 
                   return $tmpstr; 
         }
}
